package scau.zxck.view.controller;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import scau.zxck.entity.Connection;
import scau.zxck.service.TableInfoService;
import scau.zxck.utils.ViewUtil;
import scau.zxck.view.Main;
import scau.zxck.view.node.ConnTreeContextMenu;
import scau.zxck.view.node.MyTreeItem;

/**
 * Created by chenyulin on 2016/11/29.
 */
public class ConnTreeController {

    @FXML private HBox headHb;
    @FXML private TreeView connTree;
    @FXML private Button btNext;
    @FXML private Button btConnect;
    private static ConnTreeContextMenu menu;

    public void initialize() {
        Label tip = new Label("Please choose a database in a connection.");
        headHb.getChildren().clear();
        headHb.getChildren().add(tip);
        connTree.setRoot(ViewUtil.buildConnTreeRootItem());
        menu = new ConnTreeContextMenu();
        connTree.setContextMenu(menu);
        btConnect.setDisable(true);
        if (getSelectedItem().getDatabaseIndex() != -1)
            btNext.setDisable(false);
        else
            btNext.setDisable(true);
    }

    @FXML
    public void connTreeMouseClickedAction() {
        MyTreeItem selectedItem = Main.connTreeController.getSelectedItem();
        int connIndex = selectedItem.getConnIndex();
        int databaseIndex = selectedItem.getDatabaseIndex();
        if (connIndex >=0 && databaseIndex >=0) {
            menu.getItems().clear();
            btNext.setDisable(false);
            btConnect.setDisable(true);
        } else if(connIndex < 0 && databaseIndex < 0) {
            menu = new ConnTreeContextMenu();
            connTree.setContextMenu(menu);
            ConnTreeContextMenu.miConnect.setDisable(true);
            ConnTreeContextMenu.miClose.setDisable(true);
            ConnTreeContextMenu.miDelete.setDisable(true);
            ConnTreeContextMenu.miEdit.setDisable(true);
            ConnTreeContextMenu.miNew.setDisable(false);
            btNext.setDisable(true);
            btConnect.setDisable(true);
        }else if (connIndex >=0 && databaseIndex <0) {
            menu = new ConnTreeContextMenu();
            connTree.setContextMenu(menu);
            ConnTreeContextMenu.miConnect.setDisable(Main.connectionList.get(connIndex).isConnected());
            ConnTreeContextMenu.miClose.setDisable(!Main.connectionList.get(connIndex).isConnected());
            ConnTreeContextMenu.miDelete.setDisable(false);
            ConnTreeContextMenu.miEdit.setDisable(false);
            ConnTreeContextMenu.miNew.setDisable(false);
            if (Main.connectionList.get(connIndex).isConnected())
                btConnect.setText("Close");
            else
                btConnect.setText("Connect");
            btConnect.setDisable(false);
            btNext.setDisable(true);
        }
    }

    @FXML
    public void connectAction() {
        String buttonText = btConnect.getText();
        int connIndex = Main.connTreeController.getSelectedItem().getConnIndex();
        Connection conn = Main.connectionList.get(connIndex);
        if (buttonText.equals("Connect")) {
            conn.initDatabaseList();
            Main.connTreeController.initialize();
        } else if (buttonText.equals("Close")) {
            conn.getDatabaseList().clear();
            conn.setConnected(false);
            btConnect.setText("Connect");
            Main.connTreeController.initialize();
        }
    }

    @FXML
    public void nextAction() {
        MyTreeItem selectedItem = Main.connTreeController.getSelectedItem();
        int connIndex = selectedItem.getConnIndex();
        int databaseIndex = selectedItem.getDatabaseIndex();
        Connection connSelected = Main.connectionList.get(connIndex);
        String host = connSelected.getHost();
        String port = connSelected.getPort();
        String databaseName = connSelected.getDatabaseList().get(databaseIndex);
        String userName = connSelected.getUserName();
        String password = connSelected.getPassword();
        String url = "jdbc:mysql://" + host + ":" + port + "/" + databaseName;
        TableInfoService tableInfoService = new TableInfoService(url, userName, password);
        Main.tableList = tableInfoService.listTableInfo();
        Main.defaultOutDir = ViewUtil.getDefaultOutDirByConfigFile();
        Main.mainController.toTableTree();
    }

    /**
     * 获取被选中的树节点
     * @return
     */
    public MyTreeItem getSelectedItem() {
        return (MyTreeItem)connTree.getFocusModel().getFocusedItem();
    }
}
